<?php
include "mysql_config.php";
include "AliDaYuSend.php";

set_time_limit(0);
date_default_timezone_set("Asia/Shanghai");
header("Content-Type: text/html; charset=UTF-8");
$mysqlConfig = new MysqlConfig();
$mysqli = new mysqli($mysqlConfig->getMysqlServerName(), $mysqlConfig->getMysqlUserName(), $mysqlConfig->getMysqlPassword(), $mysqlConfig->getMysqlDatabase());

//修改supermarket_coupon
$fiveMinutesAgo = date('Y-m-d H:i:s', time() - 5*60);
$now = date("Y-m-d H:i:s");
$supermarketCitySql = "select cityid from 91fbw_supermarket_coupon where usetime >='".$fiveMinutesAgo."' and usetime <='".$now."' group by cityid ;";
$supermarketCityResult = $mysqli->query($supermarketCitySql);
while ($cityRow = mysqli_fetch_assoc($supermarketCityResult)) {
    $cityId = $cityRow['cityid'];
    $supermarketConfigSql = "select * from 91fbw_supermarket_config where cityid = ". $cityId . " and (status='close_send' or status='already_show')";
    $supermarketConfigResult = $mysqli->query($supermarketConfigSql);
    $remindLines = 0;
    $remindPhoneArr = [];
    $times = 0; //提醒次数
    $effectiveTime = '2017-09-01 00:00:00';
    while ($configRow = mysqli_fetch_assoc($supermarketConfigResult)) {
        $remindLines = $configRow['remindlines'];
        $remindPhoneStr = $configRow['remindphone'];
        $remindPhoneArr = explode(',', $remindPhoneStr);
        $effectiveTime = $configRow['effectivetime'];
        $couponMaxNumber = $configRow['freecouponnumber'] + $configRow['rechargecouponnumber'];
        if($remindLines != 0) {
            $times = round($couponMaxNumber*10*0.7/$remindLines);
        }
    }
    if(($remindLines == 0) || ($times == 0) || (count($remindPhoneArr)==0)) {
        continue;
    }
    $subsidiesBeforeFiveMinutes = 0;
    $subsidiesBeforeFiveMinutesSql = "select sum(subsidy) as sum from 91fbw_supermarket_coupon where cityid =".$cityId." and status=1 and created_at >= '". $effectiveTime . "' and usetime <='".$fiveMinutesAgo . "';";
    $subsidiesBeforeFiveMinutesResult = $mysqli->query($subsidiesBeforeFiveMinutesSql);
    while ($subsidiesBeforeFiveRow = mysqli_fetch_assoc($subsidiesBeforeFiveMinutesResult)) {
        $subsidiesBeforeFiveMinutes = $subsidiesBeforeFiveRow['sum'];
    }
    $subsidies = 0;
    $subsidiesSql = "select sum(subsidy) as sum from 91fbw_supermarket_coupon where cityid =".$cityId." and status=1 and created_at >= '". $effectiveTime . "' and usetime <='".$now . "';";
    $subsidiesResult = $mysqli->query($subsidiesSql);
    while ($subsidiesRow = mysqli_fetch_assoc($subsidiesResult)) {
        $subsidies = $subsidiesRow['sum'];
    }
    for($i=1; $i<=$times; $i++) {
        if((($remindLines*$i)>$subsidiesBeforeFiveMinutes) && (($remindLines*$i)<= $subsidies)) {
            //发送短信
            $cityName = '';
            $cityNameSql = "select name from 91fbw_city where id = ". $cityId. ";";
            $cityNameResult = $mysqli->query($cityNameSql);
            while ($cityNameRow = mysqli_fetch_assoc($cityNameResult)) {
                $cityName = $cityNameRow['name'];
            }
            foreach($remindPhoneArr as $Phone) {
                $money = "'".$remindLines*$i."'";
                $result = AliDaYuSend::sendSmsToAgentByCurl($Phone, $cityName, $money);
            }
            break;
        }
    }
}

$mysqli->close();
